/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.store.client.subcomponents;

import com.elf.gwt.common.client.callback.SubComponentCallback;
import com.elf.gwt.common.client.model.AccountModel;
import com.elf.gwt.common.client.utils.CommonUtils;
import com.elf.gwt.store.client.GWTCommerceConstants;
import com.elf.gwt.store.client.StoreComponentController;
import com.elf.gwt.store.client.components.AccountComponent;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;

/**
 *  Subcomponent for displaying a selection page for viewing and editing
 *  account information
 */
public class AccountViewSubComponent extends StoreSubComponent
{	
	protected String action;

	protected Hidden genderHidden;
	protected Hidden cartHidden;
	protected RadioButton genderMButton;
	protected RadioButton genderFButton;
	protected ListBox countryListBox;
	protected ListBox stateListBox;
	protected TextBox lastnameBox;
	protected TextBox firstnameBox;
	protected TextBox dobBox;
	protected Hidden dobHidden;
	protected TextBox email1Box;
	protected TextBox companyBox;
	protected TextBox streetBox;
	protected TextBox suburbBox;
	protected TextBox postCodeBox;
	protected TextBox cityBox;
	protected TextBox telephoneBox;
	protected TextBox faxBox;
	protected CheckBox newsletterBox;
	protected PasswordTextBox password1Box;
	protected PasswordTextBox password2Box;
	protected AdminFormPanel adminForm = null;

	DateTimeFormat dtformat;

	/**
	 * Constructor
	 */
	public AccountViewSubComponent(
			StoreComponentController controller, 
			SubComponentCallback callback, 
			String action)
	{
		super(controller, "AccountComponent", action, callback);
		if (accountComponent == null) accountComponent = (AccountComponent)controller.getComponent("AccountComponent");
		this.action = action;

		GWTCommerceConstants constants = controller.getStoreConstants();

		thisComponent.add(new InlineHTML("<br/>"));
		CommonUtils.addSubTitle(handle, thisComponent, constants.AccountComponentAccount());
		HorizontalPanel hpanelAccount = new HorizontalPanel();
		hpanelAccount.setStyleName(handle + "SubPanel");
		hpanelAccount.add(new Image(getImageDirectory() + "account_personal.gif"));
		boolean isAdmin = accountComponent.getAccountModel().isAdministrator();
		GWT.log("isAdmin=" + isAdmin);
		
		Grid vpanelAccount = null;
		if (isAdmin)
			vpanelAccount = new Grid(4,3);
		else 
			vpanelAccount = new Grid(3,3);
		
		InlineLabel labelAccount1 = new InlineLabel(constants.AccountComponentEditAccountInfo());
		labelAccount1.setStyleName("ComponentLink");
		labelAccount1.addClickHandler(
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						setSuperAction(AccountComponent.EditAccount);
						getCallback().onNavigate(thisComponent);
					}}
		);
		
		InlineLabel labelAccount2 = new InlineLabel(constants.AccountComponentViewAddressBook());
		labelAccount2.setStyleName("ComponentLink");
		labelAccount2.addClickHandler(
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						setSuperAction(AccountComponent.AddressBook);
						getCallback().onNavigate(thisComponent);
					}}
		);
		
		InlineLabel labelAccount3 = new InlineLabel(constants.AccountComponentChangePassword());
		labelAccount3.setStyleName("ComponentLink");
		labelAccount3.addClickHandler(
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						setSuperAction(AccountComponent.ChangePassword);
						getCallback().onNavigate(thisComponent);
					}}
		);
		vpanelAccount.setWidget(0, 0, new InlineHTML("&nbsp;"));
		vpanelAccount.setWidget(1, 0, new InlineHTML("&nbsp;"));
		vpanelAccount.setWidget(2, 0, new InlineHTML("&nbsp;"));
		vpanelAccount.setWidget(0, 1, new Image(getImageDirectory() + "arrow_green.gif"));
		vpanelAccount.setWidget(1, 1, new Image(getImageDirectory() + "arrow_green.gif"));
		vpanelAccount.setWidget(2, 1, new Image(getImageDirectory() + "arrow_green.gif"));
		vpanelAccount.setWidget(0, 2, labelAccount1);
		vpanelAccount.setWidget(1, 2, labelAccount2);
		vpanelAccount.setWidget(2, 2, labelAccount3);
		
		if (isAdmin) {
			
			InlineLabel labelAccount4 = new InlineLabel(constants.AccountComponentAdmin());
			labelAccount4.setStyleName("ComponentLink");
			labelAccount4.addClickHandler(
					new ClickHandler() {
						public void onClick(ClickEvent event) {
							if (adminForm == null) {
								adminForm = new AdminFormPanel();
								thisComponent.add(adminForm);
							}
							adminForm.initialize(accountComponent);
							adminForm.submit();
						}}
			);
			vpanelAccount.setWidget(3, 0, new InlineHTML("&nbsp;"));
			vpanelAccount.setWidget(3, 1, new Image(getImageDirectory() + "arrow_green.gif"));
			vpanelAccount.setWidget(3, 2, labelAccount4);
		}
		
		hpanelAccount.add(vpanelAccount);
		hpanelAccount.setCellWidth(vpanelAccount, "100%");
		hpanelAccount.setCellVerticalAlignment(vpanelAccount, HasVerticalAlignment.ALIGN_MIDDLE);
		
		thisComponent.add(hpanelAccount);


		thisComponent.add(new InlineHTML("<br/>"));
		CommonUtils.addSubTitle(handle, thisComponent, constants.AccountComponentOrders());
		HorizontalPanel hpanelOrders = new HorizontalPanel();
		hpanelOrders.setStyleName(handle + "SubPanel");
		hpanelOrders.add(new Image(getImageDirectory() + "account_orders.gif"));
		Grid vpanelOrders = new Grid(3,3);
		InlineLabel labelOrders1 = new InlineLabel(constants.AccountComponentViewOrders());
		labelOrders1.setStyleName("ComponentLink");
		labelOrders1.addClickHandler(
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						accountComponent.onSelection("OrdersSummaryComponent");
					}}
		);

		vpanelOrders.setWidget(0, 0, new InlineHTML("&nbsp;"));
		vpanelOrders.setWidget(0, 1, new Image(getImageDirectory() + "arrow_green.gif"));
		vpanelOrders.setWidget(0, 2, labelOrders1);
		hpanelOrders.add(vpanelOrders);
		hpanelOrders.setCellWidth(vpanelOrders, "100%");
		hpanelOrders.setCellVerticalAlignment(vpanelOrders, HasVerticalAlignment.ALIGN_MIDDLE);
		thisComponent.add(hpanelOrders);
	}
	
	public class AdminFormPanel extends FormPanel
	{
		
		public AdminFormPanel()
		{   
			super("_blank");
		    setMethod(FormPanel.METHOD_POST);
		}
		
		public void initialize(AccountComponent accountComponent)
		{
			clear();
			setAction(accountComponent.getAccountMetaModel().getAdminUrl());
			AccountModel accountModel = accountComponent.getAccountModel();
			Hidden hidden1 = new Hidden("accountId", String.valueOf(accountModel.getId()));
			Hidden hidden2 = new Hidden("accountSession", accountModel.getSession());
			Hidden hidden3 = new Hidden("languageId", String.valueOf(languagesComponent.getLanguageModel().getId()));
			FlowPanel vpanel = new FlowPanel();
			vpanel.add(hidden1);
			vpanel.add(hidden2);
			vpanel.add(hidden3);
			add(vpanel);
		}
	}
}
